<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * HITI Web Develop
 *
 * @package     HITI Web Dev
 * @category    Controllers
 * @see 		Product
 * @author      PHUOCNGUYEN - HITI Dev Team
 * @copyright   Copyright (c) 2013, HITI Corp
 */

class Product extends CI_Controller {
	var $header;
	var $footer;
	var $limit = 24;

	public function __construct()
	{
		parent::__construct();
		$this->load->model('hiti_common_model', 'hiti', true);
		$this->hiti->hiti_init();
		$this->header = $this->hiti->header_templ();
		$this->footer = $this->hiti->footer_templ();
		$this->load->model('product_model', 'pro', true);
	}

	public function index()
	{
		$this->page();
	}

	public function page($page = '')
	{
		$num = $this->pro->show('', true);
		$home = array(
			'products' => $this->pro->show($this->limit),
			'page_list' => page_list($num, $this->limit)
		);
		$GLOBALS['cfg']['site_title'] = $GLOBALS['lng']['products'];
		/*
		* Hien thi
		*/
		$this->load->view('header', $this->header);
		$this->load->view('product/index', $home);
		$this->load->view('footer', $this->footer);
	}

	public function category($id)
	{
        $info = $this->pro->category($id);
        $root1_id = get_data('product_categories', 'id = '.$info['id'], 'parent');
        $root1_key = '';
        $root1_name = '';
        $root2_id = '';
        $root2_key = '';
        $root2_name = '';
        $root3_id = '';
        $root3_key = '';
        $root3_name = '';
        if ($root1_id)
        {
            $root1_name = get_data('product_categories', 'id = '.$root1_id, 'name_vn');
            $root1_key = get_data('product_categories', 'id = '.$root1_id, 'keyword');
            $root2_id = get_data('product_categories', 'id = '.$root1_id, 'parent');
            if ($root2_id)
            {
                $root2_name = get_data('product_categories', 'id = '.$root2_id, 'name_vn');
                $root2_key = get_data('product_categories', 'id = '.$root2_id, 'keyword');
                $root3_id = get_data('product_categories', 'id = '.$root2_id, 'parent');
                if ($root3_id)
                {
                    $root3_name = get_data('product_categories', 'id = '.$root3_id, 'name_vn');
                    $root3_key = get_data('product_categories', 'id = '.$root3_id, 'keyword');
                }
            }
        }
		$num = $this->pro->show('', true, $info['id']);
		$home = array(
			'products' => $this->pro->show($this->limit, false, $info['id']),
			'page_list' => page_list($num, $this->limit),
			'info' => $info,
            'root1_id' => $root1_id,
            'root1_key' => $root1_key,
            'root1_name' => $root1_name,
            'root2_id' => $root2_id,
            'root2_key' => $root2_key,
            'root2_name' => $root2_name,
            'root3_id' => $root3_id,
            'root3_key' => $root3_key,
            'root3_name' => $root3_name,
			'list_hot' => $this->pro->show(10, false, $info['id'], '', 'hot'),
			'list_best' => $this->pro->show(10, false, $info['id'], '', 'best'),
		);
		$GLOBALS['cfg']['site_title'] = $info['name'];
		/*
		* Hien thi
		*/
		$this->load->view('header', $this->header);
		$this->load->view('product/category', $home);
		$this->load->view('footer', $this->footer);
	}

	public function view($id = '')
	{
		$method =& get_instance();
		$info = $this->pro->info($id);
		$info['info'] = str_replace('../../../upload', 'upload', $info['info']);
		$info['info'] = str_replace('../../upload', 'upload', $info['info']);
		$info['info'] = str_replace('../upload', 'upload', $info['info']);
		$info['info'] = preg_replace('/\<img(.*)src=\"(.*)\"(.*) \/>/i', "<a class='' href=\"$2\" rel=\"prettyPhoto[gallery]\"><img$1src=\"$2\"$3 class=\"thumb_big\" /></a>", $info['info']);
		$root0_id = $info['cat'];
		$root0_name = get_data('product_categories', 'id = '.$root0_id, 'name_vn');
		$root0_key = get_data('product_categories', 'id = '.$root0_id, 'keyword');
		$root1_id = get_data('product_categories', 'id = "'.$root0_id.'"', 'parent');
		$root1_key = '';
		$root1_name = '';
		$root2_id = '';
		$root2_key = '';
		$root2_name = '';
		$root3_key = '';
		$root3_name = '';
		if ($root1_id)
		{
			$root_id = $root1_id;
			$root1_name = get_data('product_categories', 'id = '.$root1_id, 'name_vn');
			$root1_key = get_data('product_categories', 'id = '.$root1_id, 'keyword');
			$root2_id = get_data('product_categories', 'id = '.$root1_id, 'parent');
			if ($root2_id)
			{
				$root_id = $root2_id;
				$root2_name = get_data('product_categories', 'id = '.$root2_id, 'name_vn');
				$root2_key = get_data('product_categories', 'id = '.$root2_id, 'keyword');
				$root3_id = get_data('product_categories', 'id = '.$root2_id, 'parent');
				if ($root3_id)
				{
					$root_id = $root2_id;
					$root3_name = get_data('product_categories', 'id = '.$root3_id, 'name_vn');
					$root3_key = get_data('product_categories', 'id = '.$root3_id, 'keyword');
				}
			}
		}
		$view_products = explode(',', $this->input->cookie('products', true));
		$home = array(
			'list_comment' => $this->list_conmnents($info['id'], 0),
			'list_others' => $this->pro->show(2, false, $info['cat']),
			'info' => $info,
			'root1_id' => $root1_id,
			'root1_key' => $root1_key,
			'root1_name' => $root1_name,
			'root2_id' => $root2_id,
			'root2_key' => $root2_key,
			'root2_name' => $root2_name,
			'root2_id' => $root2_id,
			'root3_key' => $root3_key,
			'root3_name' => $root3_name,
			'root0_id' => $root0_id,
			'root0_key' => $root0_key,
			'root0_name' => $root0_name,
			'method' => $method
		);
		$GLOBALS['cfg']['site_title'] = $info['name'];
		if ($info['des'] || $info['seo_desc']) $GLOBALS['cfg']['site_des'] = $info['seo_desc'] ? $info['seo_desc'] : str_replace("\r\n\r\n", '', trim(strip_tags($info['des'])));
		if ($info['img']) $GLOBALS['cfg']['image'] = base_url().'upload/products/thumbs/'.$info['img'];
		if ($this->input->cookie('view_product'.$info['id'], true) != 1)
		{
			setcookie('view_product'.$info['id'], 1, time() + COOKIE_TIME, '/');
			$this->pro->upview($info['id']);
		}
		/*
		* Hien thi
		*/
		$this->load->view('header', $this->header);
		$this->load->view('product/view', $home);
		$this->load->view('footer', $this->footer);
	}

	public function list_conmnents($product, $reply)
	{
		return $this->pro->list_conmnents($product, $reply);
	}
}

/* End of file product.php */
/* Location: ./application/controllers/product.php */